﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using Spotless.Services;
using System.Web.Routing;

namespace SpotlessWeb.Helpers
{
    public static class PageListHelpers
    {
        private static RouteValueDictionary GetRouteValueDictionary(HtmlHelper htmlHelper, int page)
        {
            var routeData = new RouteValueDictionary();
            foreach (var item in htmlHelper.ViewContext.HttpContext.Request.QueryString.AllKeys)
            {
                routeData.Add(item, htmlHelper.ViewContext.HttpContext.Request.QueryString[item]);
            }
            routeData["page"] = page;
            return routeData;
        }

        public static string PagingDiv(this HtmlHelper html, IPagedList list)
        {
            if (list.TotalPageCount > 1)
            {
                StringBuilder tmp = new StringBuilder();
                //var dataDictionary = html.ViewContext.RouteData.Values;

                tmp.AppendFormat("<div class=\"paging\"><div>Records {0} to {1} of {2}</div><ul>", (list.PageIndex * list.PageSize) + 1, (list.PageIndex * list.PageSize) + list.Count, list.TotalCount);

                if (list.HasPreviousPage)
                    tmp.AppendFormat("<li>{0}</li>", html.ActionLink("«", (string)null, (string)null, GetRouteValueDictionary(html, list.PageIndex - 1), null));

                var start = 0;
                var end = list.TotalPageCount;

                if (list.TotalPageCount > 10)
                {
                    start = list.PageIndex - 5;
                    if (start < 0)
                    {
                        start = 0;
                    }
                    end = start + 10;
                }

                if (end > list.TotalPageCount)
                    end = list.TotalPageCount;

                if (start > 0)
                    tmp.AppendFormat("<li>{0}</li>", html.ActionLink("...", (string)null, (string)null, GetRouteValueDictionary(null, start - 5 > 0 ? start - 5 : 0), null));

                for (int i = start; i < end; i++)
                {
                    if (i == list.PageIndex)
                        tmp.AppendFormat("<li>{0}</li>", i + 1);
                    else
                        tmp.AppendFormat("<li>{0}</li>", html.ActionLink((i + 1).ToString(), (string)null, (string)null, GetRouteValueDictionary(html, i), null));

                }
                if (end < list.TotalPageCount)
                    tmp.AppendFormat("<li>{0}</li>", html.ActionLink("...", (string)null, (string)null, GetRouteValueDictionary(html, end + 10 < list.TotalPageCount ? end + 5 : list.TotalPageCount), null));

                if (list.HasNextPage)
                    tmp.AppendFormat("<li>{0}</li>", html.ActionLink("»", (string)null, (string)null, GetRouteValueDictionary(html, list.PageIndex + 1), null));
                tmp.Append("</ul></div>");
                return tmp.ToString();
            }
            else
            {
                return "";
            }
        }

    }
}
